package com.rzj.sys_lab_assistant.controller;

import com.rzj.sys_lab_assistant.entity.Assistant;
import com.rzj.sys_lab_assistant.entity.Interview;
import com.rzj.sys_lab_assistant.entity.Student;
import com.rzj.sys_lab_assistant.entity.StudentApply;
import com.rzj.sys_lab_assistant.service.IAssistantService;
import com.rzj.sys_lab_assistant.service.IInterviewService;
import com.rzj.sys_lab_assistant.service.IStudentApplyService;
import com.rzj.sys_lab_assistant.service.IStudentService;
import com.rzj.sys_lab_assistant.utils.ResultsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @program: sys_lab_assistant
 * @description:
 * @author: 作者
 * @create: 2021-07-02 09:10
 */
@Controller
@RequestMapping("interview")
public class InterviewController {

    @Autowired
    IInterviewService interviewService;
    @Autowired
    IStudentApplyService studentApplyService;
    @Autowired
    IStudentService studentService;
    @Autowired
    IAssistantService assistantService;

    private ResultsUtils resultsUtils;

    @RequestMapping("toAdd")
    public String toAdd() {
        return "interview/interviewAdd";
    }

    @RequestMapping("toEdit/{id}")
    public String toEdit(Model model, @PathVariable("id") int id) {
        model.addAttribute("interview", interviewService.getInterviewById(id));
        return "interview/interviewUpdate";
    }

    @RequestMapping("interviewList/{postId}")
    public String interviewList(Model model, @PathVariable("postId") int postId) {
        model.addAttribute("interviewList", interviewService.InterviewsList(postId));
        return "interview/interviewList";
    }

    @RequestMapping("interviewByNoList")
    public String interviewByNoList(Model model, HttpSession session) {
        Student student = (Student) session.getAttribute("student");
        model.addAttribute("interviewList", interviewService.InterviewsByNoList(student.getStudentNo())); // 从登录用户中取值
        return "interview/interviewByNoList";
    }

    @RequestMapping("interviewAdd")
    @ResponseBody
    public Map<String, String> interviewAdd(@RequestParam("studentApplyId") String studentApplyId) {

        StudentApply studentApply = studentApplyService.getStudentApplyById(Integer.valueOf(studentApplyId));
        Student student = studentService.getStudentById(studentApply.getStudentNo());

        Interview interview = new Interview(0, student.getStudentNo(), student.getStudentName(), "0", 0);

        int flag = interviewService.addInterview(interview);
        if (flag == 1) {
            return resultsUtils.resultsMap("success", "添加成功！");
        } else {
            return resultsUtils.resultsMap("error", "添加失败，请联系管理员！");
        }
    }

    @RequestMapping("interviewEdit")
    @ResponseBody
    public Map<String, String> interviewEdit(@RequestParam("id") String id,
                                             @RequestParam("studentNo") String studentNo,
                                             @RequestParam("score") String score,
                                             @RequestParam("type") String type) {

        Interview interview = new Interview();
        interview.setId(Integer.valueOf(id));
        interview.setScore(score);
        interview.setType(Integer.valueOf(type));
        if (Integer.valueOf(type) == 1) {
            Student student = studentService.getStudentById(studentNo);
            int labId = interviewService.getLabByNo(studentNo);
            int i = assistantService.addAssistant(new Assistant(0, student.getStudentNo(), student.getStudentName(),
                    student.getStudentPwd(), student.getStudentMajor(), student.getStudentCollege(),
                    student.getStudentGrade(), student.getStudentEmail(), student.getStudentMobile(),
                    student.getImgUrl(), labId, 0));
            if (i != 1) {
                return resultsUtils.resultsMap("error", "修改失败，请联系管理员！");
            }
        }

        int flag = interviewService.updateInterview(interview);
        if (flag == 1) {
            return resultsUtils.resultsMap("success", "修改成功！");
        } else {
            return resultsUtils.resultsMap("error", "修改失败，请联系管理员！");
        }
    }

    @RequestMapping("interviewDel")
    @ResponseBody
    public Map<String, String> interviewDel(@RequestParam("id") String id) {

        int flag = interviewService.delInterview(Integer.valueOf(id));
        if (flag == 1) {
            return resultsUtils.resultsMap("success", "删除成功！");
        } else {
            return resultsUtils.resultsMap("error", "删除失败，请联系管理员！");
        }

    }

}
